package com.example.music.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.music.dto.ConsumerDto;
import com.example.music.entity.Consumer;
import com.example.music.service.Impl.ConsumerServiceImpl;
import com.example.music.utils.AjaxJson;
import com.example.music.utils.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("consumer")
public class ConsumerController {
    @Autowired
    ConsumerServiceImpl consumerService;

    @PostMapping("login")
    public AjaxJson login(@RequestBody ConsumerDto consumerDto) {
        Consumer login = consumerService.login(consumerDto);
        if (login == null) {
            return AjaxJson.getError("账号或密码错误");
        }
        return AjaxJson.getSuccessData(login);
    }

    @PostMapping("add")
    public AjaxJson add(@RequestBody Consumer consumer) {
        List<Consumer> consumers = consumerService.selectAll();
        boolean b = consumers.parallelStream().anyMatch(e -> (
                e.getUsername().equals(consumer.getUsername())
        ));
        if (b) {
            return AjaxJson.getError("用户名重复");
        }
        Integer add = consumerService.add(consumer);
        return AjaxJson.getSuccess(add.toString());
    }

    @PostMapping("update")
    public AjaxJson update(@RequestBody Consumer consumer) {
        UpdateWrapper<Consumer> consumerUpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper<Consumer> id = consumerUpdateWrapper.eq("id", consumer.getId());
        int update = consumerService.update(consumer, id);
        return AjaxJson.getSuccess(String.valueOf(update));
    }

    @PostMapping("delete")
    public AjaxJson delete(@RequestParam String id) {
        return AjaxJson.getSuccess(consumerService.deleteById(id).toString());
    }

    @PostMapping("selectById")
    public AjaxJson selectById(@RequestParam String id) {
        Consumer consumer = consumerService.selectItemById(id);
        return AjaxJson.getSuccessData(consumer);
    }

    @SaCheckLogin
    @PostMapping("selectAll")
    public AjaxJson selectAll() {
        List<Consumer> consumer = consumerService.selectAll();
        return AjaxJson.getSuccessData(consumer);
    }

    @PostMapping("updateSongPic")
    public AjaxJson updateSongPic(@RequestParam Integer id, @RequestParam("file") MultipartFile pic) {
        return consumerService.updateConsumerUrl(id, pic);
    }

    @PostMapping("sex")
    public AjaxJson updateSongPic() {
        return consumerService.sex();
    }

    @PostMapping("updateConsumerPic")
    public AjaxJson updateSingerPic(@RequestParam Integer id, @RequestParam("file") MultipartFile pic) {
        return consumerService.updateSingerPic(id,pic);
    }
}
